//
//  SearchCharViewController.swift
//  SkylowAPP
//
//  Created by 漫云 on 2024/9/22.
//

import UIKit

class SearchCharViewController: UIViewController,UISearchBarDelegate {
    
    let selectChatView = UIView()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let searchBar = UISearchBar()
        searchBar.placeholder = "Search"
        searchBar.delegate = self as? UISearchBarDelegate
        searchBar.backgroundColor = .black
        //设置代理
        searchBar.delegate = self
        //设置搜索栏的颜色
        searchBar.barTintColor = UIColor.blue
        searchBar.tintColor = UIColor.white
        
        // 如果你想改变搜索框内的文本颜色
        if let textField = searchBar.value(forKey: "searchField") as? UITextField {
            textField.textColor = .white
        }
        

        self.navigationItem.titleView = searchBar
      
        setupUI()

    }
    //当输入文字变化时调用的方法
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        print(searchText)
    }
    // MARK: --- 添加
    @objc func addSelectChatButton() {
        selectChatView.backgroundColor = UIColor.colorWith(hexString: "#272727")
        selectChatView.addRoundedCorners(withRadius: 4)
        self.view.addSubview(selectChatView)
        
        
        let closeButton = UIButton()
        closeButton.setImage(UIImage(named: "character_close_icon"), for: .normal)
        closeButton.addTarget(self, action: #selector(clickPingMuShow), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(closeButton)
        closeButton.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        let newChatButton = UIButton()
        newChatButton.setTitle("Chat in a new world", for: .normal)
        newChatButton.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        newChatButton.setTitleColor(.black, for: .normal)
//        newChatButton.titleLabel?.font = UIFont(name: "Inter", size: 12)
        newChatButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
//        newChatButton.addTarget(self, action: #selector(clickNewChatButton), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(newChatButton)
        newChatButton.snp.makeConstraints { (make) in
            make.top.equalTo(closeButton.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(34)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        selectChatView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(newChatButton.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(1)
        }
        
        let worldChatButton = UIButton()
        worldChatButton.setTitle("Chat in world sbaibxiad...", for: .normal)
        worldChatButton.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        worldChatButton.setTitleColor(.black, for: .normal)
//        worldChatButton.titleLabel?.font = UIFont(name: "Inter", size: 12)
        worldChatButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)

//        newChatButton.addTarget(self, action: #selector(clickNewChatButton), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(worldChatButton)
        worldChatButton.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(34)
        }
        
        selectChatView.snp.makeConstraints { (make) in
            make.width.equalTo(217)
//            make.height.equalTo(285)
            make.bottom.equalTo(worldChatButton.snp.bottom).offset(40)
            make.center.equalToSuperview() // view1的中心和父视图一样
        }
    }
    
    
    // MARK: --- 关闭
    @objc func clickPingMuShow() {
       // 确保布局已经完成，然后删除子视图
        selectChatView.removeFromSuperview()
    }
   
    // MARK: - homeView
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.colorWith(hexString: "#333333")
                  
      let layout = SLWaterfallFlowLayout()
      layout.delegate = self
      let collection = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
    collection.backgroundColor =  UIColor.colorWith(hexString: "#333333")
      collection.delegate = self
      collection.dataSource = self
     let nibCell = UINib(nibName: "CharCollectionCell", bundle: nil)
        collection.register(nibCell, forCellWithReuseIdentifier: "CharCollectionCell")
        
      view.addSubview(collection)
    }
}
// MARK: - home
extension SearchCharViewController: UICollectionViewDelegate, UICollectionViewDataSource {
  func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 21
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CharCollectionCell", for: indexPath) as! CharCollectionCell
      cell.chatButton.addTarget(self, action: #selector(addSelectChatButton), for: UIControl.Event.touchUpInside)
    return cell
  }
    //MARK: - 点击cell
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let charDetails = CharacterDetailsViewController()
        self.navigationController?.pushViewController(charDetails, animated: true)
    }
}

extension SearchCharViewController: SLWaterfallFlowLayoutDelegate {
    //Layout高
  func heightForRowAtIndexPath(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, indexPath: IndexPath, itemWidth: CGFloat) -> CGFloat {
      return CGFloat(354)
  }
//每行个数
  func columnNumber(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> Int {
      return 2
  }
  
  func insetForSection(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> UIEdgeInsets {
      return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
  }
  
  
  func lineSpacing(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 5
  }
  
  func interitemSpacing(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
    return 5
  }
  
  func spacingWithLastSection(collectionView collection: UICollectionView, layout: SLWaterfallFlowLayout, section: Int) -> CGFloat {
      return 0
  }
}
